package opc.read.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Description:  仙鹤的properties的配置文件，基本上废弃
 * @author: liyuan
 * @data 2021-01-06 10:23
 */
@Deprecated
public class ReadTItem2 {
    public static Map<String, String> loadFile() throws IOException {
        InputStream is = ReadTItem2.class.getClassLoader().getResourceAsStream("config_zxw.properties");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        Properties properties = new Properties();
        properties.load(br);

        String host = properties.getProperty("host");
        String domain = properties.getProperty("domain");
        String user = properties.getProperty("username");
        String password = properties.getProperty("password");
        String clsid = properties.getProperty("clsid");
        String progid=properties.getProperty("progid");
        int len = Integer.parseInt(properties.getProperty("len"));
        Map<String, String> map = new HashMap<String, String>(len * 3 + 4);

        map.put("host", host);
        map.put("domain", domain);
        map.put("user", user);
        map.put("password", password);
        map.put("clsid", clsid);
        map.put("len", properties.getProperty("len"));
        map.put("progid",progid);
        for (int i = 0; i < len; i++) {
            String deviceName = properties.getProperty("deviceName" + i);
            String itemName = properties.getProperty("deviceName" + i + ".deviceItem");
            map.put("deviceName" + i, deviceName);
//            map.put("deviceName" + i + ".deviceItem", itemName);
            /***
             * 配置文件少了.pv的适配
             */
            String[] names = new String[]{"F1_OU1S_", "F1_OU2S_", "F1_OU3S_", "F1_OUT1_", "F1_OUT2_", "F1_OUT3_",
                    "F3_OUT1_", "F3_OUT2_", "F4_OUT1_", "F4_OUT2_", "F5_OUT1_", "F5_OUT2_", "F5_OUT3_", "IN"};
            String[] split = itemName.split(",");
            StringBuilder builder = new StringBuilder();
            for (int j = 0; j < split.length - 1; j++) {
                if ("PM28_PXT28850".equals(split[j]) || "PM28_SET_WIDTH".equals(split[j]) ||
                        "PM28_WIDTH".equals(split[j]) || "PM28_JIANFENGDIAN".equals(split[j]) ||
                        "PM28_GAOFENGDIAN".equals(split[j]) || "PM28_DIGUDIAN".equals(split[j]) ||
                        "PM28_CN_H".equals(split[j]) || "PM28_FT28900_BC".equals(split[j])) {
                    builder.append(split[j] + ",");
                } else if ("PM28_CN".equals(split[j]) || "PM28_ZQ".equals(split[j]) || "PM28_BJQS".equals(split[j]) ||
                        "PM28_ZZQS".equals(split[j]) || "PM28_GAS_Z".equals(split[j]) ||
                        "PM28_FT28878B".equals(split[j]) || "PM28_JF".equals(split[j]) ||
                        "PM28_GF".equals(split[j]) || "PM28_DG".equals(split[j])) {
                    for (int count = 0; count < names.length; count++) {
                        builder.append(split[j] + ",");
                    }
                } else {
                    builder.append(split[j] + ",");
                }
            }
            // 最后一项
            if ("PM28_PXT28850".equals(split[split.length - 1]) || "PM28_SET_WIDTH".equals(split[split.length - 1]) ||
                    "PM28_WIDTH".equals(split[split.length - 1]) || "PM28_JIANFENGDIAN".equals(split[split.length - 1]) ||
                    "PM28_GAOFENGDIAN".equals(split[split.length - 1]) || "PM28_DIGUDIAN".equals(split[split.length - 1]) ||
                    "PM28_CN_H".equals(split[split.length - 1]) || "PM28_FT28900_BC".equals(split[split.length - 1])) {
                builder.append(split[split.length - 1]);
            } else if ("PM28_CN".equals(split[split.length - 1]) || "PM28_ZQ".equals(split[split.length - 1]) || "PM28_BJQS".equals(split[split.length - 1]) ||
                    "PM28_ZZQS".equals(split[split.length - 1]) || "PM28_GAS_Z".equals(split[split.length - 1]) ||
                    "PM28_FT28878B".equals(split[split.length - 1]) || "PM28_JF".equals(split[split.length - 1]) ||
                    "PM28_GF".equals(split[split.length - 1]) || "PM28_DG".equals(split[split.length - 1])) {
                for (int count = 0; count < names.length - 1; count++) {
                    builder.append(split[split.length - 1] + ",");
                }
                builder.append(split[split.length - 1]);
            } else {
                builder.append(split[split.length - 1]);
            }

            map.put("deviceName" + i + ".deviceItem", builder.toString());
        }
        if(br!=null){
            try{
                br.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(is!=null){
            try{
                is.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return map;
    }
}
